<?php

namespace App\Http\Controllers\Resource;

use Illuminate\Support\Facades\Log;

/**
 * 新语听书 数字资源
 *
 * 未提供目录接口
 */
class XytsController extends CommonController
{
    public $access_id = '89';
    public $account_id = '108';
    public $secret_key = 'MFFTWKb4pZ1S7XPS2eq$';
    //  public $class_type_id = '1469'; //只能获取此类数据
    public $host_url = 'https://api.xinyulib.com.cn/openapi';

    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 分类接口-获取所有分类
     *
     */
    public function getType()
    {
        $eno = strtoupper(md5($this->access_id . date('Y-m-d H') . $this->secret_key));
        $url = $this->host_url . '/getclassifylist?access_id=' . $this->access_id . '&enc=' . $eno;
        $result = request_url($url);
        $result = json_decode($result, true);
        if ($result['code'] !== 1) {
            Log::channel('xytslog')->debug('资源获取失败');
            Log::channel('xytslog')->debug($result);
            Log::channel('xytslog')->debug('--------------------------------------------------------');
            return []; //没数据时，返回的false
        }
        if (empty($result['data'])) {
            return [];
        }
        return  $result;
    }



    /**
     * 书籍接口-获取分类下包含的书籍
     * @param  classTypeId
     * @param  sign MD5签名
     * @param  pageNum 页码
     * @param  pageSize 每页大小
     */
    public function getTypeBookList($classify_id, $page = 1, $limit = 10)
    {
        $eno = strtoupper(md5($this->access_id . $classify_id . $limit . $page . date('Y-m-d H') . $this->secret_key));
        $url = $this->host_url . '/getclassifybooklist?access_id=' . $this->access_id . '&enc=' . $eno . '&classify_id=' . $classify_id . '&page_num=' . $page . '&page_amount=' . $limit;
        $result = request_url($url);
        $result = json_decode($result, true);

        if ($result['code'] !== 1) {
            Log::channel('xytslog')->debug('资源获取失败');
            Log::channel('xytslog')->debug($result);
            Log::channel('xytslog')->debug('--------------------------------------------------------');
            return []; //没数据时，返回的false
        }
        if (empty($result['data'])) {
            return [];
        }
        return  $result;
    }



    /**
     * 章节接口-根据书籍ID获取书籍章节信息（分页）
     * @param  book_id
     */
    public function getBookChapter($book_id)
    {
        $a = strtoupper(md5($book_id . $this->access_id . date('Y-m-d H') . $this->secret_key));
        $url = $this->host_url . '/getbookchapterlist?AccessID=' . $this->access_id . '&a=' . $a . '&Book_Id=' . $book_id;
        $result = request_url($url);
        $result = json_decode($result, true);
        if ($result['code'] !== 1) {
            Log::channel('xytslog')->debug('资源获取失败');
            Log::channel('xytslog')->debug($result);
            Log::channel('xytslog')->debug('--------------------------------------------------------');
            return []; //没数据时，返回的false
        }
        if (empty($result['data'])) {
            return [];
        }
        $data = [];
        foreach ($result['data'] as $key => $val) {
            $data['data'][$key]['resource_business_id'] = 6;
            $data['data'][$key]['book_id'] = $val['Book_Id'];
            $data['data'][$key]['sort'] = $val['Chapter_Sort']; //排序
            $data['data'][$key]['chapter_id'] = $val['Chapter_Id'];
            $data['data'][$key]['chapter_name'] = $val['Chapter_Title'];
            $data['data'][$key]['chapter_duration'] = $val['Chapter_Duration']; //时长
            $data['data'][$key]['chapter_size'] = null; //大小
        }

        $data['total'] = count($data['data']);
        $data['per_page'] = 1;
        $data['current_page'] = 1;

        return $data;
    }

    /**
     * 音频接口-获取音频跳转地址的URL
     * @param  book_id
     * @param  sign MD5签名
     */
    public function getAudioSkipInfo($book_id)
    {
        $enc = strtoupper(md5($book_id . $this->access_id . $this->account_id . date('Y-m-d H') . $this->secret_key));
        $url = 'https://api.xinyulib.com/open/authdetail.jsp?access_id=' . $this->access_id . '&account_id=' . $this->account_id . '&book_id=' . $book_id . '&enc=' . $enc;

        return  $url;
    }
}
